//
//  ProductThumbView.m
//  PurchasingMeeting
//
//  Created by zhuang yihang on 7/17/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "ProductThumbView.h"
#import "ProductObject.h"
#import "DataManager.h"
#import "PMGlobal.h"

@implementation ProductThumbView
{
    ShopObject* selectedshop;
}
@synthesize view;
@synthesize imageview;
@synthesize object = obj;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
//        self.layer.borderWidth = 2;
//        self.layer.borderColor = [UIColor blackColor].CGColor;
    }
    return self;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (id)initWithProduct:(ProductObject *)object tag:(int)tag{
    numtag = tag;
    self.object = object;
    /*
#ifdef DHH_iOS
    self = [self initWithFrame:CGRectMake(0, 0, 145, 150)];
    obj = object; 
    if (self) {
          [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(numreload:) name:@"numreload" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(collectreload:) name:@"collectreload" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(uncollectreload:) name:@"uncollectreload" object:nil];
        
        self.imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
       // self.imageview.image = [[UIHelper getInstance] getBundleImage:@"detail.png"];
        [self addSubview:self.imageview];
        
        
        self.view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 120, 145, 30)];
        self.view.image = [[UIHelper getInstance] getBundleImage:@"image.png"];
        [self addSubview:self.view];
        
        self.price = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 60, 15)];
        NSString *str = [NSString stringWithFormat:@"￥%.0f",object.pSale];
        self.price.text = str;
        self.price.backgroundColor = [UIColor clearColor];
        self.price.textColor = [UIColor whiteColor];
        self.price.font = [UIFont systemFontOfSize:12];
        [self.view addSubview:self.price];
        
        self.good = [[UIImageView alloc]initWithImage:[[UIHelper getInstance] getBundleImage:@"good.png"]];
        self.good.frame = CGRectMake(70, 5, [[UIHelper getInstance] getBundleImage:@"good.png"].size.width, [[UIHelper getInstance] getBundleImage:@"good.png"].size.height);
     //   [self.view addSubview:self.good];
        
        if ([[DataManager sharedManager]productIsFavorite:obj Shopid:nil]>0) {
        self.star = [[UIImageView alloc]initWithImage:[[UIHelper getInstance] getBundleImage:@"star.png"]];
        self.star.frame = CGRectMake(90, 5, [[UIHelper getInstance] getBundleImage:@"star.png"].size.width, [[UIHelper getInstance] getBundleImage:@"star.png"].size.height);
        [self.view addSubview:self.star];
        }
        
        self.num = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 20)];
        self.num.text = object.pCode;
        self.num.backgroundColor = [UIColor clearColor];
        self.num.textColor = [UIColor whiteColor];
        self.num.font = [UIFont systemFontOfSize:14];
        [self.view addSubview:self.num];
        
        self.num1 = [[UILabel alloc]initWithFrame:CGRectMake(115, 10, 50, 20)];
        int num = [[DataManager sharedManager]productIsFavorite:obj Shopid:nil];
        NSString *numtext = [NSString stringWithFormat:@"%d",num];
        if (num>0) {
            self.num1.text = numtext;
            self.line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 145, 1)];
            self.line.backgroundColor = [UIColor greenColor];
            [self.view addSubview:self.line];
        }
        self.num1.backgroundColor = [UIColor clearColor];
        self.num1.textColor = [UIColor greenColor];
        self.num1.font = [UIFont systemFontOfSize:12];
        [self.view addSubview:self.num1];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [self addGestureRecognizer:tap];
        self.userInteractionEnabled = YES;
        
    }
#else
     */
    UIImage *img = [[UIHelper getInstance] getBundleImage:@"product_thumb_bg.png"];
#ifdef DHH_iOS
    self = [self initWithFrame:CGRectMake(0, 0, 145, 150)];
#else
    if (tag==10) {
        self = [self initWithFrame:CGRectMake(0, 0, img.size.width*1.3, img.size.height*1.3)];
    }
    else
    {
        self = [self initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
    }
#endif
    obj = object;
    selectedshop = [[DataManager sharedManager]getCurrentShop];
    
    if (self) {
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(numreload:) name:@"numreload" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(collectreload:) name:@"collectreload" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(uncollectreload:) name:@"uncollectreload" object:nil];
        
        typestr = [[PMGlobal getInstance]gettype];
        if ([typestr isEqualToString:@"type1"]) {
            float border = 1;
            self.imageview = [[UIImageView alloc]initWithFrame:CGRectMake(border, border,
                                                                          self.width-2*border, self.height-2*border-25)];
            //  self.imageview.image = [[UIHelper getInstance] getBundleImage:@"detail.png"];
            //img = [[UIHelper getInstance] getBundleImage:@"image.png"];;
            self.view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.imageview.width, 25)];
            ///        self.view.image = img;
            [self.view setImage:[[UIHelper getInstance] getBundleImage:@"productviewbottom.png"]];
            imageview.contentMode = UIViewContentModeScaleAspectFill;
            self.imageview.layer.borderWidth = 1;
            self.imageview.layer.borderColor = [UIColor colorWithWhite:0.7 alpha:0.5].CGColor;
            self.view.bottom = self.height;
            [self addSubview:self.view];
            
            [self addSubview:self.imageview];
            self.imageview.clipsToBounds = YES;
            
            self.num = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, self.width-5, self.view.height)];
            NSString *str = [NSString stringWithFormat:@"%@",obj.pCode];
            self.num.text = str;
            self.num.centerY = self.view.height/2+2;
//            self.num.adjustsFontSizeToFitWidth = YES;
            self.num.backgroundColor = [UIColor clearColor];
//            self.num.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            if ([obj.n_status intValue]  == 1 ) {
                self.num.textColor = [UIColor redColor];
            }
            else
            {
                self.num.textColor = [UIColor blackColor];
            }
            self.num.font = [UIFont systemFontOfSize:12];
            [self.view addSubview:self.num];
            
            UIImage *img = [[UIHelper getInstance] getBundleImage:@"priceimage.png"];
            self.priceimageview = [[UIImageView alloc]initWithImage:img];
            self.priceimageview.frame = CGRectMake(0, 0, img.size.width*0.5, img.size.height*0.5);
//            self.priceimageview.transform = CGAffineTransformMakeScale(0.7, 0.7);
            [self addSubview:self.priceimageview];
            
            self.price = [[UILabel alloc]initWithFrame:CGRectMake(-10, 13, 50, self.view.height/2)];
            str = [NSString stringWithFormat:@"￥%.0f",object.pSale];
            self.price.text = str;
            self.price.textAlignment = NSTextAlignmentCenter;
            self.price.backgroundColor = [UIColor clearColor];
            if ([obj.n_status intValue]) {
                self.price.textColor = [UIColor redColor];
            }
            else
            {
                self.price.textColor = [UIColor whiteColor];
            }
            [self.price setTransform:CGAffineTransformMakeRotation(-M_PI/4)];
            self.price.font = [UIFont boldSystemFontOfSize:PRODUCT_THUMB_SIZE];
            [self.priceimageview addSubview:self.price];
            
            self.colorid = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 70, self.view.height/2)];
            str = [NSString stringWithFormat:@"%@",obj.pName];
            self.colorid.text = str;
            self.colorid.centerY = self.view.height/2;
            self.colorid.adjustsFontSizeToFitWidth = YES;
            self.colorid.textAlignment = UITextAlignmentRight;
            self.colorid.backgroundColor = [UIColor clearColor];
            if ([obj.n_status intValue] == 1 ) {
                self.colorid.textColor = [UIColor redColor];
            }
            else
            {
                self.colorid.textColor = [UIColor blackColor];
            }
            self.colorid.right = self.view.width-5;
            self.colorid.font = [UIFont systemFontOfSize:12];
            [self.view addSubview:self.colorid];
            
            
//            if (arc4random()%2==0) {
//                img = [[UIHelper getInstance] getBundleImage:@"good.png"];
//                self.good = [[UIImageView alloc]initWithImage:img];
//                self.good.frame = CGRectMake(90, 15, img.size.width, img.size.height);
//                //  [self.view addSubview:self.good];
//                self.good.center = CGPointMake(0, self.view.height/2);
//                self.good.left = 75;
//            }
            
            img = [[UIHelper getInstance] getBundleImage:@"star.png"];
            self.star = [[UIImageView alloc]init];
            self.star.frame = CGRectMake(115, 15, img.size.width*5, img.size.height);
            [self.imageview addSubview:self.star];
            self.star.center = CGPointMake(0, self.view.height/4);
            self.star.left = self.width-63;
            

            [self collectreload:nil];
            
            [self refreshOrderNumber];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
            [self addGestureRecognizer:tap];
            self.userInteractionEnabled = YES;

        }
        else
        {
            UIImageView *bgV = [[UIImageView alloc] initWithImage:img];
#ifdef DHH_iOS
            bgV.frame = self.frame;
#endif
            [self addSubview:bgV];
            float border = 1;
            self.imageview = [[UIImageView alloc]initWithFrame:CGRectMake(border, border,
                                                                          self.width-2*border, self.height-2*border)];
          //  self.imageview.image = [[UIHelper getInstance] getBundleImage:@"detail.png"];
            //img = [[UIHelper getInstance] getBundleImage:@"image.png"];;
            self.view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.imageview.width, 33)];
    ///        self.view.image = img;
            imageview.contentMode = UIViewContentModeScaleAspectFill;
            self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
            self.view.bottom = self.imageview.height;
            [self.imageview addSubview:self.view];

            [self addSubview:self.imageview];
            self.imageview.clipsToBounds = YES;
            
            self.num = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.width/2-5, self.view.height/2)];
            self.num.text = object.pCode;
            self.num.adjustsFontSizeToFitWidth = YES;
            self.num.backgroundColor = [UIColor clearColor];
            if ([obj.n_status intValue]  == 1 ) {
                self.num.textColor = [UIColor redColor];
            }
            else
            {
                self.num.textColor = [UIColor whiteColor];
            }
            self.num.font = [UIFont systemFontOfSize:12];
            [self.view addSubview:self.num];
            
            self.price = [[UILabel alloc]initWithFrame:CGRectMake(10, self.num.bottom, 70, self.view.height/2)];
            NSString *str = [NSString stringWithFormat:@"￥%.2f",object.pSale];
            self.price.text = str;
            self.price.backgroundColor = [UIColor clearColor];
            if ([obj.n_status intValue]) {
                self.price.textColor = [UIColor redColor];
            }
            else
            {
                self.price.textColor = [UIColor whiteColor];
            }
            self.price.font = [UIFont systemFontOfSize:12];
            [self.view addSubview:self.price];
            
            self.colorid = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 70, self.view.height/2)];
            self.colorid.text = object.pColor;
            self.colorid.textAlignment = UITextAlignmentRight;
            self.colorid.backgroundColor = [UIColor clearColor];
            if ([obj.n_status intValue] == 1 ) {
                self.colorid.textColor = [UIColor redColor];
            }
            else
            {
                self.colorid.textColor = [UIColor whiteColor];
            }
            self.colorid.right = self.view.width-2;
            self.colorid.font = [UIFont systemFontOfSize:12];
            [self.view addSubview:self.colorid];

            
//            if (arc4random()%2==0) {
//                img = [[UIHelper getInstance] getBundleImage:@"good.png"];
//                self.good = [[UIImageView alloc]initWithImage:img];
//                self.good.frame = CGRectMake(90, 15, img.size.width, img.size.height);
//              //  [self.view addSubview:self.good];
//                self.good.center = CGPointMake(0, self.view.height/2);
//                self.good.left = 75;
//            }

            img = [[UIHelper getInstance] getBundleImage:@"star.png"];
            self.star = [[UIImageView alloc]init];
            self.star.frame = CGRectMake(115, 15, img.size.width*5, img.size.height);
            [self.imageview addSubview:self.star];
            self.star.center = CGPointMake(0, self.view.height/4);
            self.star.left = self.width-63;
            
            if ([[DataManager sharedManager]productIsFavorite:obj Shopid:selectedshop.shopID]>0) {
                for (int i=0; i<[[DataManager sharedManager]productIsFavorite:obj Shopid:selectedshop.shopID]; i++) {
                    UIImageView *starimageview = [[UIImageView alloc]initWithImage:img];
                    starimageview.frame = CGRectMake(115, 0, img.size.width, img.size.height);
                    [self.star addSubview:starimageview];
                   // starimageview.center = CGPointMake(0, self.view.height/2);
                    starimageview.left = img.size.width*i;
                }
            }
            
            
            self.num1 = [[UILabel alloc]initWithFrame:CGRectMake(140, 15, 50, 20)];
            self.num1.backgroundColor = [UIColor clearColor];
            if ([obj.n_status intValue] == 1) {
                self.num1.textColor = [UIColor redColor];
            }
            else
            {
                self.num1.textColor = [UIColor greenColor];
            }
            self.num1.font = [UIFont systemFontOfSize:12];
            self.num1.right = self.view.width-2;
            self.num1.textAlignment = NSTextAlignmentRight;
            [self.view addSubview:self.num1];
            
            [self refreshOrderNumber];
            

            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
            [self addGestureRecognizer:tap];
            self.userInteractionEnabled = YES;
        }
    }
//#endif
    return self;
}


- (void)tap:(UITapGestureRecognizer *)gesuture{
    ProductObject *product = [[DataManager sharedManager] getProduct:obj.pCode withColorID:obj.pColorCode];
    if (numtag==0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"goToProductDetail"
                                                            object:product];
    }else if(numtag==10){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"goToProductDetail1"
                                                            object:product];
    }
    else if(numtag ==1)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"goToProductDetail1"
                                                            object:product];
    }
}

- (void)refreshOrderNumber{

    if ([typestr isEqualToString:@"type1"]) {
        if (self.ordernumimageview) {
            [self.ordernumimageview removeFromSuperview];
            self.ordernumimageview = nil;
        }
        ShopObject *shopobj=[[DataManager sharedManager]getCurrentShop];
        int orderNumber = [[DataManager sharedManager]getOrderAmount :obj Shopid:shopobj.shopID];
        if (orderNumber>0) {
            UIImage *img = [[UIHelper getInstance] getBundleImage:@"ordernumimage.png"];
            self.ordernumimageview = [[UIImageView alloc]initWithImage:img];
            self.ordernumimageview.frame = CGRectMake(0, 0, img.size.width, img.size.height);
            self.ordernumimageview.right = self.width+5;
            self.ordernumimageview.top = 0;
            [self addSubview:self.ordernumimageview];
            
            self.num1 = [[UILabel alloc]initWithFrame:CGRectMake(140, 15, 50, 20)];
            self.num1.backgroundColor = [UIColor clearColor];
            if ([obj.n_status intValue] == 1) {
                self.num1.textColor = [UIColor redColor];
            }
            else
            {
                self.num1.textColor = [UIColor whiteColor];
            }
            self.num1.text = [NSString stringWithFormat:@"%d",orderNumber];
            self.num1.font = [UIFont boldSystemFontOfSize:14];
            self.num1.right = self.view.width-2;
            self.num1.textAlignment = NSTextAlignmentCenter;
            self.num1.center = CGPointMake(self.ordernumimageview.width/2, self.ordernumimageview.height/2);
            [self.ordernumimageview addSubview:self.num1];
        }
    }
    else
    {
        if (self.line) {
            [self.line removeFromSuperview];
            self.line = nil;
        }
        ShopObject *shopobj=[[DataManager sharedManager]getCurrentShop];
        int orderNumber = [[DataManager sharedManager]getOrderAmount :obj Shopid:shopobj.shopID];
        if (orderNumber>0) {
            self.num1.text = [NSString stringWithFormat:@"%d",orderNumber];
            self.line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 1)];
            
            //   if (numtag ==2) {
            //       self.line.backgroundColor = [UIColor redColor];
            //   }
            if (numtag!=2){
                if ([obj.n_status intValue]==1) {
                    self.line.backgroundColor = [UIColor redColor];
                }
                else
                {
                    self.line.backgroundColor = [UIColor greenColor];
                }
            }
            
            [self.view addSubview:self.line];
        }
        else
        {
            self.num1.text = @"";
        }
    }
}

-(void)numreload:(NSNotification *)sender
{

    ProductObject * product = sender.object;

    if ([product isTheSameProduct:obj]) {
        [self refreshOrderNumber];
    }
}


-(void)collectreload:(NSNotification *)sender
{
   // ProductObject * product = sender.object;
   // if ([product isTheSameProduct:obj]) {
//        UIImage *img = [[UIHelper getInstance] getBundleImage:@"star.png"];
//        [self.star removeAllSubviews];
//        for (int i=0; i<[[DataManager sharedManager]productIsFavorite:obj Shopid:selectedshop.shopID]; i++) {
//            UIImageView *starimageview = [[UIImageView alloc]initWithImage:img];
//            starimageview.frame = CGRectMake(self.width-48, 0, img.size.width, img.size.height);
//            [self.star addSubview:starimageview];
//           // starimageview.center = CGPointMake(0, self.view.height/2);
//            starimageview.left = img.size.width*i;
//        }
   // }
    
    UIImage *img = [[UIHelper getInstance] getBundleImage:@"star.png"];
    if ([[DataManager sharedManager]productIsFavorite:obj Shopid:selectedshop.shopID]>0) {
        for (int i=0; i<[[DataManager sharedManager]productIsFavorite:obj Shopid:selectedshop.shopID]; i++) {
            UIImageView *starimageview = [[UIImageView alloc]initWithImage:img];
            starimageview.frame = CGRectMake(115, 0, img.size.width, img.size.height);
            [self.star addSubview:starimageview];
            // starimageview.center = CGPointMake(0, self.view.height/2);
            starimageview.left = self.star.width - (img.size.width*i+5);
            starimageview.bottom = self.imageview.bottom-5;
        }
    }
}

-(void)uncollectreload:(NSNotification *)sender
{
   // ProductObject * product = sender.object;
   // if ([product isTheSameProduct:obj]) {
        [self.star removeAllSubviews];
   // }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
